﻿using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class CategoryCertificateService : IEntityService<DMChungChi>
    {
        public List<DMChungChi> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.DMChungChis.ToList();
            }
        }

        public bool Add(DMChungChi pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entities.DMChungChis.Add(new DMChungChi()
                {
                    TenChungChi = pb.TenChungChi
                });
                entities.SaveChanges();
                return true;
            }

        }

        public bool Update(DMChungChi pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.DMChungChis.Where(x => x.MaDMChungChi == pb.MaDMChungChi).FirstOrDefault();
                if (query != null)
                {

                    query.TenChungChi = pb.TenChungChi;
                    query.ChuyenNganh = pb.ChuyenNganh;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(DMChungChi pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.DMChungChis.Where(x => x.MaDMChungChi == pb.MaDMChungChi).FirstOrDefault();
                if (query != null)
                {
                    try
                    {
                        entities.DMChungChis.Remove(query);
                        entities.SaveChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                    }
                }
                return false;
            }
        }

        public bool DeleteList(List<DMChungChi> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (DMChungChi cc in list)
                {
                    var query = entities.DMChungChis.Where(x => x.MaDMChungChi == cc.MaDMChungChi).FirstOrDefault();
                    if (query != null)
                    {
                        entities.DMChungChis.Remove(query);
                    }
                }
                try
                {
                    entities.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                }
                return false;
            }
        }

        public List<DMChungChi> GetPaging(int pageIndex, int pageSize)
        {
            var list = new List<DMChungChi>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.DMChungChis.OrderBy(p => p.MaDMChungChi).Skip((pageIndex - 1) * pageSize).Take(pageSize);

                foreach (var item in query)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.DMChungChis.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }
    }
}
